<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div id="reader"></div>
<div style="font-size:45px;">
    <label>切换摄像头：</label>
    <select id="cameras" onchange="changeCameras()" style="font-size: 45px; height: 70px;">

    </select>
    <label style="color: red">注：如果默认开启的摄像头不是您想使用的摄像头，请通过切换下拉菜单中的摄像头来切换您想使用的摄像头进行扫描二维码。</label><br>
    <a th:href="@{/goIndex}" style="padding-top: 25px;">返回首页</a>
</div>
<script th:src="@{/js/html5-qrcode.min.js}"></script>
<script th:inline="javascript">
    //前缀
    let ctx = [[@{/}]];
        //类型
        let type = [[${type}]];

    /**根据设备id开启摄像头*/
    function start(id) {
        const html5QrCode = new Html5Qrcode("reader");
        html5QrCode.start(
            id,
            {
                fps: 10,
                qrbox: 600
            },
            qrCodeMessage => {
                html5QrCode.stop().then(ignore => {
                    qrCodeMessage=qrCodeMessage.replace("/","◐")
                    if(qrCodeMessage.indexOf('|')){
                        //获取最后一个字符
                        let jumpType = qrCodeMessage.substring(qrCodeMessage.lastIndexOf('|') + 1);
                        let codeArr = qrCodeMessage.split("|");
                        let partId = codeArr[2];
                        let typeNum = codeArr[3];
                        for (let i = 0; i < partId.length; i++) {
                            let partIdChar = partId.charAt(i);
                            if (partIdChar != '0' &&
                                partIdChar != '1' &&
                                partIdChar != '2' &&
                                partIdChar != '3' &&
                                partIdChar != '4' &&
                                partIdChar != '5' &&
                                partIdChar != '6' &&
                                partIdChar != '7' &&
                                partIdChar != '8' &&
                                partIdChar != '9'
                            ) {
                                alert("二维码识别失败，请重新扫码！");
                                location.href="/index";
                                break;
                            }
                        }
                        for (let i = 0; i < typeNum.length; i++) {
                            let typeChar = typeNum.charAt(i);
                            if (typeChar != '1' && typeChar != '2') {
                                alert("二维码识别失败，请重新扫码！");
                                location.href="/index";
                                break;
                            }
                        }
                        //如果是1就调取转序的跳转页面的接口
                        if (jumpType === '1') {
                            location.href = ctx + 'result/' + type + '/' + qrCodeMessage;
                        }
                        //如果是2就调取装配的跳转页面的接口
                        else if (jumpType === '2') {
                            location.href = ctx + 'assembly/' + type + '/' + qrCodeMessage;
                        }
                        else {
                            alert("二维码识别失败，请重新扫码！");
                            location.href="/index";
                        }
                    }
                    else {
                        alert("二维码识别失败，请重新扫码！");
                        location.href="/index";
                    }
                });
            },
            errorMessage => {
                // parse error, ideally ignore it. For example:
                // console.log(`QR Code no longer in front of camera.`);
                // alert("无法打开摄像头！");
            })
            .catch(err => {
                // Start failed, handle it. For example,
                // console.log(`Unable to start scanning, error: ${err}`);
                // alert("无法打开摄像头！");
            });
    }

    /**扫描所有摄像头*/
    Html5Qrcode.getCameras().then(devices => {
        if (devices) {
            let cameras = document.getElementById("cameras");
            cameras.options.length = 0;
            //将所有摄像头的id和我自定义的摄像头名称赋予给下拉框
            for (let i = 0; i < devices.length; i++) {
                cameras.options[cameras.options.length] = new Option("第" + eval(i + 1) + "个摄像头", devices[i].id); // 在最后一个值后面添加多一个
            }
            //如果摄像头超过一个则使用第二摄像头（一般情况下第二摄像头是后摄像头，为了兼容低端手机。像多摄像头的手机就需要用下拉菜单选择使用哪个摄像头了。）
            if (devices.length > 1) {
                start(devices[1].id);
            }
            //如果只有一个摄像头就调用第一个摄像头
            else {
                start(devices[0].id);
            }
        }
    }).catch(err => {
        alert("摄像头设备获取失败！");
    });

    /**
     * 切换摄像头
     */
    function changeCameras() {
        start(document.getElementById("cameras").value);
    }

    // 插件源码地址 https://github.com/mebjas/html5-qrcode
    // 插件文档 https://blog.minhazav.dev/HTML5-QR-Code-scanning-launched-v1.0.1/
</script>
</body>
</html>
